﻿using Gzh.Core;
using Gzh.Core.Constant;
using Gzh.DAL.Infrastructure;
using Gzh.DAL.Models;
using Gzh.Lib;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gzh.DAL.DAO
{
    /// <summary>
    /// ${table.comment}
    /// </summary>
    public class ${ClassName}DAO : BaseRepository<${TableName}>
    {

        //列表
        public JArray Page(int page, int rows, ref int total, ref int records)
        {
            if (page<1) page = 1;
            if (rows>50) rows = 50;
            var query = from tb1 in dbSet

                        select new
                        {
                           <#list table.columns  as column>
                          <#if column_has_next>
                            tb1.${column.columnName},    //${column.columnComment}
                           <#else>
                            tb1.${column.columnName}    //${column.columnComment}
                           </#if>
                           </#list>
                        };

            //过滤
            records = query.Count();
            if (records < 1)
            {
                return new JArray();
            }

            total = (int)Math.Ceiling(Convert.ToDouble(records) / rows);

            var data = query.OrderByDescending(x => x.create_dt).Skip((page - 1) * rows).Take(rows).ToList().Select(x => new
            {
            <#list table.columns  as column>
             <#if column_has_next>
                ${column.columnName2} = x.${column.columnName}.ToOutput(),//${column.columnComment}
             <#else>
                ${column.columnName2} = x.${column.columnName}.ToOutput()//${column.columnComment}
             </#if>
            </#list>
            });

            return data.ToJArray();
        }



        //-------------------------------------------------------------------------------------------------------------------        

        //详情
        public JObject GetInfoById(long id)
        {
            var query = from tb1 in dbSet

                        where tb1.id == id
                        select new
                        {
                         <#list table.columns  as column>
                         <#if column_has_next>
                           tb1.${column.columnName},  //${column.columnComment}
                         <#else>
                           tb1.${column.columnName}  //${column.columnComment}
                         </#if>
                         </#list>
                        };

            var data = query.ToList().Select(x => new
            {
            <#list table.columns  as column>
            <#if column_has_next>
                ${column.columnName2} = x.${column.columnName}.ToOutput(),//${column.columnComment}
            <#else>
                ${column.columnName2} = x.${column.columnName}.ToOutput()//${column.columnComment}
            </#if>
            </#list>
            }).FirstOrDefault();
            return data.ToJObject();
        }



         /// <summary>
            /// ${table.comment}
            /// </summary>
            public class ${ClassName}DAO : BaseRepository<${TableName}>
            {

                //列表
                public JArray Page(int page, int rows, ref int total, ref int records)
                {
                    if (page<1) page = 1;
                    if (rows>50) rows = 50;
                    var query = from tb1 in dbSet

                                select new
                                {
                                   <#list table.columns  as column>
                                  <#if column_has_next>
                                    tb1.${column.columnName},    //${column.columnComment}
                                   <#else>
                                    tb1.${column.columnName}    //${column.columnComment}
                                   </#if>
                                   </#list>
                                };

                    //过滤
                    records = query.Count();
                    if (records < 1)
                    {
                        return new JArray();
                    }

                    total = (int)Math.Ceiling(Convert.ToDouble(records) / rows);

                    var data = query.OrderByDescending(x => x.create_dt).Skip((page - 1) * rows).Take(rows).ToList().Select(x => new
                    {
                    <#list table.columns  as column>
                     <#if column_has_next>
                        ${column.columnName} = x.${column.columnName},//${column.columnComment}
                     <#else>
                        ${column.columnName} = x.${column.columnName}//${column.columnComment}
                     </#if>
                    </#list>
                    });

                    return data.ToJArray();
                }



    }
}
